#!/bin/bash
[% c("var/set_default_env") -%]
builddir=/var/tmp/build
mkdir $builddir
distdir=/var/tmp/dist/[% project %]
mkdir -p "$distdir"
tar -C /var/tmp/dist -xf [% c('input_files_by_name/cmake') %]
tar -C /var/tmp/dist -xf [% c('input_files_by_name/ninja') %]
tar -C $distdir -xf [% c('input_files_by_name/clang') %]
tar -C $distdir -xf [% c('input_files_by_name/cctools') %]
tar -C $builddir -xf [% c('input_files_by_name/llvm-project') %]

patch -p1 < $rootdir/tools.diff

sysrootdir=$distdir/MacOSX[% c("version") %].sdk/
mkdir -p $sysrootdir
cd $sysrootdir
python3 "$rootdir/unpack-sdk.py" "$rootdir/[% c('input_files_by_name/SDK') %]" "Library/Developer/CommandLineTools/SDKs/MacOSX[% c('version') %].sdk"

arch=[% c("var/macos_arch") %]
clangdir=$distdir/clang/bin
cctoolsdir=$distdir/cctools/bin
target=[% c("var/build_target") %]
# We still need to put the cctoolsdir on the path. That's because of `lipo`. See
# the respective comment in the cctools build script.
export PATH="/var/tmp/dist/ninja:/var/tmp/dist/cmake/bin:$cctoolsdir:$PATH"

# See tor-browser.git/taskcluster/scripts/misc/build-llvm-common.sh
export MACOSX_DEPLOYMENT_TARGET=[% c("var/macosx_deployment_target") %]
target=[% c("var/build_target") %]
[% IF c("var/macos-aarch64") -%]
  # wrapper taken from:
  clangwrappersdir=$distdir/clang/wrappers
  mkdir $clangwrappersdir
  compiler_wrapper() {
    echo exec $clangdir/$1 -mcpu=apple-m1 \"\$@\" > $clangwrappersdir/$1
    chmod +x $clangwrappersdir/$1
  }
  compiler_wrapper clang
  compiler_wrapper clang++
  clang=$clangwrappersdir/clang
  arch=arm64
[% ELSE -%]
  clang=$clangdir/clang
  arch=x86_64
[% END -%]

COMMON_CMAKE_FLAGS="
  -DCMAKE_BUILD_TYPE=Release
  -DCMAKE_INSTALL_PREFIX=$distdir/clang/lib/clang/[% pc("llvm-project", "var/llvm_major") %]/
  -DCMAKE_C_COMPILER=$clang
  -DCMAKE_CXX_COMPILER=$clang++
  -DCMAKE_AR=$clangdir/llvm-ar
  -DCMAKE_RANLIB=$clangdir/llvm-ranlib
  -DCMAKE_LINKER=$clangdir/ld64.lld
  -DCMAKE_LIPO=$clangdir/llvm-lipo
  -DCMAKE_C_COMPILER_TARGET=$target
  -DCMAKE_CXX_COMPILER_TARGET=$target
  -DCMAKE_ASM_COMPILER_TARGET=$target
  -DCMAKE_SYSTEM_NAME=Darwin
  -DCMAKE_SYSTEM_VERSION=$MACOSX_DEPLOYMENT_TARGET
  -DCMAKE_OSX_SYSROOT=$sysrootdir
  -DCMAKE_EXE_LINKER_FLAGS=-fuse-ld=lld
  -DCMAKE_SHARED_LINKER_FLAGS=-fuse-ld=lld
  -DDARWIN_osx_ARCHS=$arch
  -DDARWIN_osx_SYSROOT=$sysrootdir
  -DDARWIN_macosx_OVERRIDE_SDK_VERSION=11.0
  -DDARWIN_osx_BUILTIN_ARCHS=$arch
  -DLLVM_DEFAULT_TARGET_TRIPLE=$target
  -DLLVM_ENABLE_ASSERTIONS=OFF
  -DLLVM_CONFIG_PATH=$clangdir/llvm-config
  "

mkdir $builddir/faketools

# compiler-rt build script expects to find `codesign` in $PATH.
# Give it a fake one.
echo "#!/bin/sh" > $builddir/faketools/codesign
chmod +x $builddir/faketools/codesign

# cmake makes decisions based on the output of the mac-only sw_vers, which is
# obviously missing when cross-compiling, so create a fake one. The exact
# version doesn't really matter: as of writing, cmake checks at most for 10.5.
echo "#!/bin/sh" > $builddir/faketools/sw_vers
echo echo 10.12 >> $builddir/faketools/sw_vers
chmod +x $builddir/faketools/sw_vers

export PATH=$PATH:"$builddir/faketools"
# End of Moz's tricks

mkdir build_compiler_rt
cd build_compiler_rt

# And we build compiler-rt by following taskcluster/scripts/misc/build-compiler-rt.sh.
cmake $builddir/clang-source/compiler-rt -GNinja \
  $COMMON_CMAKE_FLAGS

ninja -j[% c("num_procs") %] -v install

cd $builddir/clang-source

cmake -GNinja -S runtimes -B build \
      -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
      $COMMON_CMAKE_FLAGS

cd build
ninja -j[% c("num_procs") %] -v install

cd $distdir/cctools/bin
ln -s ../../clang/bin/clang $target-clang
ln -s ../../clang/bin/clang++ $target-clang++

cd /var/tmp/dist
[% c('tar', {
   tar_src => [ project ],
   tar_args => '-caf ' _ dest_dir _ '/' _ c('filename'),
}) %]
